﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Controls;
using VAdvantage.Model;

using VAdvantage.UIEvent;
using System.Windows.Media.Imaging;
using VAdvantage.Grids;

namespace VAdvantage.Editor
{
    public class WStringEditor : WEditor,IContextMenuListener
    {

        private String oldValue;

        private ContextMenuStrip popupMenu;

        private bool tableEditor = false;

        private VTextBox txtControl = null;

        private ComboBox cmbControl = null;

        private bool isAutoComplete;
        
        public WStringEditor()
            : this("String", false, false, true, 30, 30, "", null)
        {
        }

        public WStringEditor(GridField gridField)
            : this(gridField, false)
        {
           
        }

        public WStringEditor(GridField gridField, bool tableEditor)
            : base(gridField.IsAutocomplete() ? new ComboBox() as Control : new VTextBox() as Control, gridField)
        {

            //System.Windows.Application.LoadComponent(rs, new System.Uri("/VAdvantage;component/Resrces.xaml", System.UriKind.Relative));

            this.tableEditor = tableEditor;
            isAutoComplete = gridField.IsAutocomplete();

            if (isAutoComplete)
            {
                cmbControl = (ComboBox)GetControl();
            }
            else
            {
                txtControl = (VTextBox)GetControl();
            }

            Init(gridField.GetObscureType());
        }


        private void Init(string obscureType)
        {
            if (gridField != null)
            {
                //getComponent().setMaxlength(gridField.getFieldLength());

                txtControl.MaxLength = gridField.GetFieldLength();

                int displayLength = gridField.GetDisplayLength();
                if (displayLength <= 0 || displayLength > MAX_DISPLAY_LENGTH)
                {
                    displayLength = MAX_DISPLAY_LENGTH;
                }

                

                popupMenu = new ContextMenuStrip(false, false, true);

                MenuItem editor = new MenuItem();

                editor.Header =  "Editor";
                editor.Icon = popupMenu.GetIcon("Editor");
               
                editor.Name =  "Editor";
                editor.Click += delegate
                {
                    OnMenu(new ContextMenuEvent(editor.Name));
                };
                popupMenu.Items.Add(editor);

                SetPopup(popupMenu);
            }
        }

       

       
        public WStringEditor(String columnName, bool mandatory, bool isReadOnly, bool isUpdateable,
           int displayLength, int fieldLength, String vFormat, String obscureType)
        :base(new VTextBox(), columnName, null, null, mandatory, isReadOnly, isUpdateable)
        {
            txtControl = (VTextBox)GetControl();
            Init(obscureType);
        }

        public override void SetReadWrite(bool readWrite)
        {
           // GetControl().SetReadonly(!readWrite);
        }

        public override bool IsReadWrite()
        {
            return false;
            //return !GetControl().IsReadonly();
        }

        public override void SetValue(object value)
        {
            GetControl().SetValue(value);
        }

        public override object GetValue()
        {
            return GetControl().GetValue();
        }

        public override string GetDisplay()
        {
            return "";
        }

        #region "IContextmenuEvent"

        public void OnMenu(ContextMenuEvent evt)
        {

            if (ContextMenuStrip.PREFERENCE_EVENT.Equals(evt.GetContextEvent()))
            {
                //if (MRole.getDefault().isShowPreference())
                  //  ValuePreference.start(this.getGridField(), getValue());
                MessageBox.Show("Preference");
            }
            else if ("Editor".Equals(evt.GetContextEvent()))
            {
                MessageBox.Show("Editor");
            }
        }
        #endregion


        #region "IEventListener"
        public override void OnEvent(Event e)
        {
            
        }
#endregion
    }
}